using System;
using System.Collections;
using System.Collections.Generic;
using Nirvana.SkillEditor;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;

namespace Nirvana.SkillEditor
{
    public class SkillEditorWindow : EditorWindow
    {
        private static PlayableAsset _selectPlayableAsset;
        [SerializeField] private TreeViewState _treeViewState;
        private SkillFileTreeView _treeView;
        private SearchField _searchField;

        [MenuItem("NirvanaToolkit/Skill Editor")]
        private static void Open()
        {
            var window = GetWindow<SkillEditorWindow>();
            window.Focus();
            window.Show();
        }

        public static void SetSelectPlayableAsset(PlayableAsset playableAsset)
        {
            _selectPlayableAsset = playableAsset;
        }

        private void OnEnable()
        {
            _treeViewState ??= new TreeViewState();
            _treeView = new SkillFileTreeView(_treeViewState);
            _searchField = new SearchField();
        }

        private void OnGUI()
        {
            DoTopGUI();
            DoToolbar();
            DoTreeView();
        }

        void DoTopGUI()
        {
            GUILayout.BeginHorizontal();
            
            if (GUILayout.Button("Export Json (All File)"))
            {
                if (EditorUtility.DisplayDialog("提示", "确认导出全部技能文件？", "确认", "取消"))
                {
                    SkillActionBuilder.BuildSkillSources();
                    SkillActionBuilder.BuildSkillEnumCSharpScript();
                }
            }

            if (GUILayout.Button("Export Json (Select File)"))
            {
                SkillActionBuilder.BuildSkillSource(_selectPlayableAsset);
                SkillActionBuilder.BuildSkillEnumCSharpScript();
            }
            
            GUILayout.EndHorizontal();
        }

        void DoToolbar()
        {
            GUILayout.BeginHorizontal(EditorStyles.toolbar);
            GUILayout.Space(3);
            _treeView.searchString = _searchField.OnToolbarGUI(_treeView.searchString);
            GUILayout.EndHorizontal();
        }

        private void DoTreeView()
        {
            EditorGUILayout.BeginHorizontal();

            if (GUILayout.Button("ExpandAll"))
            {
                _treeView.ExpandAll();
                GUIUtility.ExitGUI();
            }

            if (GUILayout.Button("CollapseAll"))
            {
                _treeView.CollapseAll();
                GUIUtility.ExitGUI();
            }

            if (GUILayout.Button("Delete Selects"))
            {
                _treeView.DeleteSelects();
                GUIUtility.ExitGUI();
            }

            if (GUILayout.Button("Refresh Data"))
            {
                _treeView.Reload();
                GUIUtility.ExitGUI();
            }

            EditorGUILayout.EndHorizontal();

            var rect = GUILayoutUtility.GetRect(0, 1000, 0, 1000);
            _treeView.OnGUI(rect);
        }
    }
}